﻿<div class="ph-tabs">
    <ul>
        <li class="k-state-active">学校列表</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <div style="display: inline-block;">
                <select id="selBureau"></select>
            </div>
            <span class="k-textbox k-space-right">
                <input type="text" placeholder="请输入学校名称" id="txtKey" />
                <a href="javascript:Filter()" class="k-icon k-i-search">&nbsp;</a>
            </span>
            @if (PH.Web.Global.WebContext.CurrentUser.Roles.Any(p => p.RoleName == "BureauAdmin")){
                <button class="k-button" onclick="CreateNewSchool()"><span class="k-icon k-i-plus"></span><span>添加新的学校</span></button>
            }
        </div>
        <div id="grid_school" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("单位管理", "学校列表");

    var bureaus = Whale.Remote.GetEntity({
        url: "/Systems/Bureau/GetAllChidren",
        parameters: {
            withme: true
        }
    });

    $("#selBureau").kendoDropDownList({
        dataTextField: "EduName",
        dataValueField: "ID",
        dataSource: bureaus,
        change: function (e) {
            Filter();
        }
    });
</script>

<script class="grid_school_columns">
    var schoolFields = [{
        title: "编码",
        field: "Code",
        width: 80
    }, {
        title: "学校名称",
        field: "SchoolName",
        attributes: { style: "text-align:left" },
        width: 260
    }, {
        title: "类型",
        field: "SchoolType",
        width: 100
    }, {
        title: "人数",
        field: "PersonCount",
        width: 50
    }, {
        title: "用户名",
        field: "User.UserName",
        width: 100
    }, {
        title: "姓名",
        field: "User.RealName",
        width: 100
    }, {
        title: "联系手机",
        field: "User.Mobile",
        width: 100
    }, {
        title: "Email",
        field: "User.Email",
        template: "#: User.Email # <a href='javascript:ChangeEmail(\"#:User.Guid#\")' class='k-icon k-i-pencil'>&nbsp;</a>",
        width: 200
    }, {
        title: "添加时间",
        template: "#=kendo.toString(kendo.parseDate(CreatedOn),'yyyy-MM-dd')#",
        width: 100
        @if (PH.Web.Global.WebContext.CurrentUser.Roles.Any(p => p.RoleName == "BureauAdmin")) {
            <text>
            }, {
                title: "操作",
                template: "#=GetOperations(Guid)#",
                attributes: { style: "text-align:left" },
                width: 300
            </text>
        }
    }, {}];

    function GetOperations(guid) {
        var html = "<button class='k-button' onclick=\"EditSchool('" + guid + "')\"><span class='k-icon k-i-pencil'></span><span>修改</span></button>";
        html += "<button class='k-button' onclick=\"DeleteSchool('" + guid + "')\"><span class='k-icon k-i-close'></span><span>删除</span></button>";
        html += "<button class='k-button' onclick=\"CreateForignClass('" + guid + "')\"><span class='k-icon k-i-insert-m'></span><span>建外籍班级</span></button>";
        return html;
    }

    function CreateNewSchool() {
        wndGeneralEdit.setOptions({ title: "添加新的学校" });
        wndGeneralEdit.refresh({ url: "/School/SchoolInfo/Edit" });
        wndGeneralEdit.open();
    }

    function EditSchool(guid) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改学校信息" });
            wndGeneralEdit.refresh({ url: "/School/SchoolInfo/Edit/" + guid });
            wndGeneralEdit.open();
        }
    }

    function DeleteSchool(guid) {
        if (confirm("真的要删除这个学校吗？")) {
            Whale.Remote.Operation({
                url: "/School/SchoolInfo/Remove",
                parameters: { guid: guid },
                success: function (result) {
                    if ($("#grid_school").length > 0)
                        $("#grid_school").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }

    function CreateForignClass(guid) {
        Whale.Remote.Operation({
            url: "/School/Class/CreateForignClass",
            parameters: { schGuid: guid },
            success: function (result) {
                wndGeneralInformation.content("已成功创建外籍班级").open();
            },
            error: function (msg) {
                wndGeneralInformation.content(msg).open();
            }
        });
    }

    function ChangeEmail(guid) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改帐号Email" });
            wndGeneralEdit.refresh({ url: "/Accounts/User/Email/" + guid });
            wndGeneralEdit.open();
        }
    }

</script>

<script class="grid_config">
    $(function () {
        $("#grid_school").kendoGrid({
            columns: schoolFields,
            dataSource: Whale.dataSource({
                url: "/School/SchoolInfo/GetSchools",
                getParameters: function () {
                    return {
                        key: $.trim($("#txtKey").val()),
                        bureauID: $("#selBureau").data("kendoDropDownList").value(),
                        getUser: true,
                        getSchoolType: true,
                        getPersonCount:true
                    };
                },
                pageSize: 15

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function Filter() {
        $("#grid_school").data("kendoGrid").dataSource.page(0);
    }
</script>
